﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NumberLoacation
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btSearch_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtPhoneNum.Text.Trim()))
            {
                labPhoneLocal.Content="请输入号码";
                txtPhoneNum.Clear();
                return;
            }
            if (txtPhoneNum.Text.Length!=11)
            {
                labPhoneLocal.Content = "请输入正确的号码";
                txtPhoneNum.Clear();
                return;
            }
            PhoneNum phN = new PhoneNum();
            DataTable NumTable = SqlHelper.ExecuteDataTable(
                      @"select TelType,TelArea from T_TelNum where 
                        StartTelNum =@startNum ",
                new SqlParameter[] { new SqlParameter("@startNum", txtPhoneNum.Text.Remove(6, 4)) });
            
            if (NumTable.Rows.Count<=0)
            {
                labPhoneLocal.Content="没有数据";
                txtPhoneNum.Clear();
                return;
            }
            phN.TelType = (string)NumTable.Rows[0][0];
            phN.City = (string)NumTable.Rows[0][1];
            phN.NumLocation = phN.City + phN.TelType;
            //labPhoneLocal.DataContext = phN;     //如果上次操作修改了labPhoneLocal.Content，那么此次的绑定将不会有效果
            labPhoneLocal.Content = phN.NumLocation;
        }
    }
}
